const app = getApp()
Page({
    data: {
        navData: [],
        navItemList:[
            { 
                title: '全部',
                id:'all',
                SelecttemList:[
                    { nam: '全部', id: 0 },
                    { nam: '投融资', id: 2 },
                    { nam: '前期开发', id: 3 },
                    { nam: '定位策划', id: 4 },
                    { nam: '设计管理', id: 5 },
                    { nam: '成本招采', id: 6 },
                    { nam: '工程管理', id: 7 },
                    { nam: '营销管理', id: 8 },
                    { nam: '社区运营', id: 9 },
                    { nam: '品牌客服', id: 10 },
                    { nam: '全精装', id: 41 },
                    { nam: '定位策划', id: 12 },
                    { nam: '设计工程', id: 13 },
                    { nam: '商业物业', id: 14 },
                    { nam: '产业地产', id: 16 },
                    { nam: '文旅地产', id: 17 },
                    { nam: '战略运营', id: 20 },
                    { nam: '企业管理', id: 21 },
                    { nam: '产品线', id: 22 },
                    { nam: '财务税筹', id: 23 },
                    { nam: '人力资源', id: 24 },
                    { nam: '行政会议', id: 25 },
                    { nam: '信息化', id: 26 },
                    { nam: '办公技巧', id: 27 },
                    { nam: '通用管理', id: 28 },
                    { nam: '法务', id: 40 },
                    { nam: '制度类', id: 30 },
                    { nam: '销售类', id: 31 },
                    { nam: '服务类', id: 32 },
                    { nam: '产品类', id: 33 },
                    { nam: '竞品类', id: 34 },
                    { nam: '行业类', id: 35 },
                    { nam: '培训管理类', id: 36 },
                    { nam: '市场管理类', id: 37 },
                    { nam: '咨询类', id: 38 },
                    { nam: '在线产品类', id: 39 }
                ]
            },
            {
                 title: '住宅地产',
                  id: 'all',
                SelecttemList: [
                    { nam: '投融资', id: 2 },
                    { nam: '前期开发', id: 3 },
                    { nam: '定位策划', id: 4 },
                    { nam: '设计管理', id: 5 },
                    { nam: '成本招采', id: 6 },
                    { nam: '工程管理', id: 7 },
                    { nam: '营销管理', id: 8 },
                    { nam: '社区运营', id: 9 },
                    { nam: '品牌客服', id: 10 },
                    { nam: '全精装', id: 41 }
                ] 
            },
            { 
                title: '商业地产',
                id: 'all', 
                SelecttemList:[
                    { nam: '定位策划', id: 12 },
                    { nam: '设计工程', id: 13 },
                    { nam: '商业物业', id: 14 }
                ]
            },
            { 
                title: '其他业态', 
                id: 'all',
                SelecttemList:[
                    { nam: '产业地产', id: 16 },
                    { nam: '文旅地产', id: 17 }
                ]
            },
            { 
                title: '管理类',
                 id: 'all' ,
                SelecttemList:[
                    { nam: '战略运营', id: 20 },
                    { nam: '企业管理', id: 21 },
                    { nam: '产品线', id: 22 },
                    { nam: '财务税筹', id: 23 },
                    { nam: '人力资源', id: 24 },
                    { nam: '行政会议', id: 25 },
                    { nam: '信息化', id: 26 },
                    { nam: '办公技巧', id: 27 },
                    { nam: '通用管理', id: 28 },
                    { nam: '法务', id: 40 },
                ]
            },
            {
                title: '内部课程专业',
                id: 'all',
                SelecttemList: [
                    { nam: '制度类', id: 30 },
                    { nam: '销售类', id: 31 },
                    { nam: '服务类', id: 32 },
                    { nam: '产品类', id: 33 },
                    { nam: '竞品类', id: 34 },
                    { nam: '行业类', id: 35 },
                    { nam: '培训管理类', id: 36 },
                    { nam: '市场管理类', id: 37 },
                    { nam: '咨询类', id: 38 },
                    { nam: '在线产品类', id: 39 }
                ]
            }
        ],
        SelectNavIndex:0,
        SelectShow:false,
        SelectIndex:0,
        catalogSelect: 0,
        navIndex: 0,
        searchtype: false,
        searchinput: '',
        tokenID: '',
        title: '',
        duration: 2000,
        maskShow: false,
        page: 1,
        datamsg: false,
        moment: [],
        historylist: [],
        seeklist: [],
        hotkeyword: [],
        is_modal_Hidden: true,
        is_modal_Msg: '请输入课程密码',
        passwordValue: '',
        courseLinkUrl: '',
        password:'',
        momentData:false
    },
    onLoad: function(options) {

        var _this = this;
        _this.setData({
            tokenID: app.data.tokenID
        })
        _this.onReachBottom();
        var historylist = wx.getStorageSync('historylist');
        var arr = [];
        if (historylist != '') {
            for (var i in JSON.parse(wx.getStorageSync('historylist'))) {
                arr.push(JSON.parse(wx.getStorageSync('historylist'))[i])
            }
            _this.setData({
                historylist: arr
            })
        }
        wx.request({
            url: app.data.api + 'Curriculum/classification',
            method: "POST",
            data: {
                "token": _this.data.tokenID,
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                var arr = [],
                    list = res.data.data;
                for (var i = 0; i < list.length; i++) {
                    arr.push(list[i])
                }
                _this.setData({
                    navData: arr
                })
            }
        })
        wx.request({
            url: app.data.api + 'Curriculum/hotkeyword',
            method: "POST",
            data: {
                "token": _this.data.tokenID,
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                var arr = [],
                    list = res.data.data;
                if (res.data.status) {
                    for (var i = 0; i < list.length; i++) {
                        arr.push(list[i])
                    }
                    _this.setData({
                        hotkeyword: arr
                    })
                }
            }
        })
    },
    onPullDownRefresh: function() {
        var _this = this;
        this.setData({
            page: 1
        })
        wx.showNavigationBarLoading();
        _this.gtemomentList("down")
    },
    onReachBottom: function() {
        var that = this;
        that.gtemomentList("up")
    },
    gtemomentList: function(type) {
        wx.showLoading({
            title: '玩命加载中',
        })
        var that = this;
        that.setData({
            tokenID: app.data.tokenID
        })
        wx.request({
            url: app.data.api + 'Curriculum/list',
            data: {
                "page": that.data.page,
                "token": that.data.tokenID,
                "name": that.data.searchinput,
                "classid": that.data.catalogSelect,
            },
            method: "POST",
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status) {
                    if (type == 'up') {
                        var moment_list = that.data.moment;
                        for (var i = 0; i < res.data.data.list.length; i++) {
                            moment_list.push(res.data.data.list[i]);
                        }
                        if (res.data.data.list.length >= 10) {
                            that.setData({
                                datamsg: false
                            })
                        }else{
                            that.setData({
                                datamsg: true
                            })
                        }
                  
                        that.setData({
                            page: that.data.page + 1,
                            moment: that.data.moment
                        })
                    } else {
                        if (res.data.data.list.length >= 10) {
                            that.setData({
                                datamsg: false
                            })
                        } else {
                            that.setData({
                                datamsg: true
                            })
                        }
                        var moment_list = [];
                        for (var i = 0; i < res.data.data.list.length; i++) {
                            moment_list.push(res.data.data.list[i]);
                        }
                        that.setData({
                            page: that.data.page + 1,
                            moment: moment_list
                        })
                        wx.stopPullDownRefresh();
                    }
                    if (that.data.moment.length > 0)
                    {
                        that.setData({
                            momentData:true
                        })
                    }else{
                        that.setData({
                            momentData: false
                        })
                    }
                    wx.hideLoading();
                    wx.hideNavigationBarLoading();
                } else {
                    wx.hideLoading();
                    wx.showModal({
                        title: '提示',
                        content: res.data.msg,
                        showCancel: false,
                        confirmColor: '#04A9F4',
                        success: function(res) {
                            if (res.confirm) {
                                wx.redirectTo({
                                    url: '../login/login'
                                })
                            } else if (res.cancel) {
                                wx.redirectTo({
                                    url: '../login/login'
                                })
                            }
                        }
                    });
                }
            }
        })
    },
    showSearch: function() {
        var _this = this,
            maskShow = _this.data.maskShow;
        this.setData({
            maskShow: !maskShow,
            SelectShow:false
        })
    },
    tapactive: function(event) {
        var that = this;
        that.setData({
            catalogSelect: event.currentTarget.dataset.id,
            navIndex: event.currentTarget.dataset.index,
            page:1,
            datamsg:false
        })
        that.gtemomentList("down")
    },
    searchClassify: function (event){
        var _this = this;
        _this.setData({
            SelectIndex: event.currentTarget.dataset.index,
            SelectShow: true,
            SelectNavIndex:null
        })
    },
    SelectHide: function (){
        var _this = this;
        
        _this.setData({
            SelectShow: false
        })
    },
    SelectNavItem: function (event){
        var _this = this;
        var index = event.currentTarget.dataset.index;
        var SelectIndex = event.currentTarget.dataset.selectindex;
        var id = event.currentTarget.dataset.id;
        _this.setData({
            SelectNavIndex: index,
            catalogSelect:id,
            page: 1,
            searchinput:'',
            datamsg: false
        })
        _this.SelectHide();
        _this.gtemomentList("down")
    },
    changeText: function(event) {
        var _this = this;
        if (event.detail.value.length > 0) {
            _this.setData({
                searchinput: event.detail.value
            })
        } else {
            _this.setData({
                searchtype: false,
                searchinput: ''
            })
        }
    },
    emptysearch: function() {
        var _this = this;
        _this.setData({
            searchinput: '',
            searchtype: false
        })
    },
    searchCourse: function() {
        var _this = this,
            arr = [];
        arr.push(_this.data.searchinput);
        arr = arr.concat(_this.data.historylist)
        var historyarr = [...new Set(arr)];
        if (_this.data.searchinput) {
            _this.setData({
                historylist: historyarr,
                searchtype: true,
                page: 1
            })
            _this.gtemomentList();
            wx.setStorageSync('historylist', JSON.stringify(_this.data.historylist));
        } else {
            wx.showModal({
                title: '提示',
                content: '请输入搜索关键词',
                showCancel: false,
                confirmColor: '#04A9F4',
                success: function(res) {
                    if (res.confirm) {
                    } else if (res.cancel) {
                    }
                }
            })
            return false;
        }
    },
    deleteHistory: function(event) {
        var _this = this,
            type = event.currentTarget.dataset.id;
        if (type == 'historylist') {
            _this.setData({
                historylist: [],
            })
            wx.removeStorageSync("historylist")
        } else {
            var arr = _this.data.historylist;
            arr.splice(type, 1);
            _this.setData({
                historylist: arr,
            })
            wx.setStorageSync('historylist', JSON.stringify(_this.data.historylist))
        }
    },
    jumpplay: function(event) {

    },
    pushinputval: function(event) {
        var _this = this;
        _this.setData({
            searchinput: event.target.dataset.text
        })
        _this.searchCourse();
    },
    onMyEvent: function(event) {
        var _this = this;
        this.setData({
            passwordValue: event.detail.passwordValue,
        })
        if (_this.data.passwordValue == _this.data.password)
        {
            wx.navigateTo({
                url: _this.data.courseLinkUrl
            })
            _this.setData({
                is_modal_Hidden: true,
                password: '',
                courseLinkUrl: ''
            })
        }else{
            wx.showToast({
                title: '课程密码有误！请重新输入。',
                icon: 'none',
                duration: 800
            })
        }
 
    },
    courseLink(event) {
        var _this = this;
        var url = event.currentTarget.dataset.url;
        var needpwd = event.currentTarget.dataset.needpwd;
        var password = event.currentTarget.dataset.password;
        if (needpwd == 1000)
        {
            wx.navigateTo({
                url: url
            })
        }else{
            _this.setData({
                is_modal_Hidden: false,
                password: password,
                courseLinkUrl: url
            })
        }

    },
    _imgOnLoad:function(event){
    }
})